package com.yc.game.service.impl;

import com.yc.game.dao.MoneyMapper;
import com.yc.game.dao.TrainingContentMapper;
import com.yc.game.pojo.Money;
import com.yc.game.pojo.TrainingContent;
import com.yc.game.service.MoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MoneyServiceImpl implements MoneyService {
    @Autowired
    private MoneyMapper moneyMapper;
    @Autowired
    private TrainingContentMapper trainingContentMapper;

    /**
     * 判断保证金是否充足
     * @param email
     * @return
     */
    @Override
    public boolean getByEmail(String email, Integer trainingId) {
        TrainingContent bz = trainingContentMapper.selectByPrimaryKey(trainingId);
        Money money = moneyMapper.getByEmail(email);
        if (money.getMoney() - bz.getMoney() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 减少金额
     * @param email
     * @param trainingId
     */
    @Override
    public void subMoneyByEmail(String email, Integer trainingId) {
        Money money = moneyMapper.getByEmail(email);
        TrainingContent bz = trainingContentMapper.selectByPrimaryKey(trainingId);
        moneyMapper.updateByEmail(email, money.getMoney() - bz.getMoney());
    }
}
